package wo.bsys.test;

import java.util.Arrays;
import java.util.Date;

import javax.annotation.Resource;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import wo.bsys.po.Menu;
import wo.bsys.po.Role;
import wo.bsys.po.User;
import wo.bsys.repository.DictionaryRepository;
import wo.bsys.repository.MenuRepository;
import wo.bsys.repository.RoleRepository;
import wo.bsys.repository.UserRepository;
import wo.common.util.WoUtil;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class BSysInitializer {
	private Logger LOG = LogManager.getLogger(BSysInitializer.class);

	@Resource
	private MenuRepository menuRepository;

	@Resource
	private DictionaryRepository dictionaryRepository;

	@Resource
	private RoleRepository roleRepository;

	@Resource
	private UserRepository userRepository;

	private final static String PWD = "123456";

	/**
	 * 初始化系统数据
	 */
	@Test
	public void initData() {
		// 系统管理
		// 父菜单
		Menu m = new Menu("esys", "系统管理", "00", "fa-bars", null, null);
		this.menuRepository.save(m);
		// 子菜单
		Menu c1 = new Menu("esys-menu", "菜单", "0010", "fa-gears", "menu/main.jsp", m);
		this.menuRepository.save(c1);
		Menu c2 = new Menu("esys-role", "角色", "0020", "fa-group", "role/main.jsp", m);
		this.menuRepository.save(c2);
		Menu c3 = new Menu("esys-user", "用户", "0030", "fa-user", "user/main.jsp", m);
		this.menuRepository.save(c3);
		Menu c4 = new Menu("esys-dictionary", "数据字典", "0040", "fa-book", "dictionary/main.jsp", m);
		this.menuRepository.save(c4);
		// 角色：系统管理员
		Role admin = new Role();
		admin.setId("admin");
		admin.setName("系统管理员");
		roleRepository.save(admin);
		// 管理员用户
		User userAdmin = new User();
		userAdmin.setId("admin");
		userAdmin.setLoginName("admin");
		userAdmin.setPassword(WoUtil.getMD5(userAdmin.getId(), PWD));
		userAdmin.setCreateTime(new Date());
		userAdmin.setPasswordTime(new Date());
		userAdmin.setRoles(Arrays.asList(admin));
		userRepository.save(userAdmin);
		// 父菜单：resume管理
		Menu resume = new Menu("resume", "resume管理", "10", "fa-exclamation-circle", null, null);
		this.menuRepository.save(resume);
		// resume子菜单
		Menu resumeResume = new Menu("resume-resume", "简历", "1020", "fa-gears", "resume/main.jsp", resume);
		this.menuRepository.save(resumeResume);
		// 角色：resume
		Role resumeRole = new Role();
		resumeRole.setId("resume");
		resumeRole.setName("resume");
		resumeRole.setMenus(Arrays.asList(resume, resumeResume));
		roleRepository.save(resumeRole);
		// 用户：resume
		User resumeUser = new User();
		resumeUser.setId("resume");
		resumeUser.setLoginName("resume");
		resumeUser.setPassword(WoUtil.getMD5(resumeUser.getId(), PWD));
		resumeUser.setCreateTime(new Date());
		resumeUser.setPasswordTime(new Date());
		resumeUser.setRoles(Arrays.asList(resumeRole));
		userRepository.save(resumeUser);
		// 父菜单：department管理
		Menu department = new Menu("department", "department管理", "20", "fa-exclamation-circle", null, null);
		this.menuRepository.save(department);
		// department子菜单
		Menu departmentDepartment = new Menu("department-department", "部门", "2010", "fa-gears", "department/main.jsp",
				department);
		this.menuRepository.save(departmentDepartment);
		Menu departmentPosition = new Menu("department-position", "职务", "2020", "fa-gears", "position/main.jsp",
				department);
		this.menuRepository.save(departmentPosition);
		Menu departmentStaff = new Menu("department-staff", "人员", "2030", "fa-gears", "staff/main.jsp", department);
		this.menuRepository.save(departmentStaff);
		// 角色：department
		Role departmentRole = new Role();
		departmentRole.setId("department");
		departmentRole.setName("department");
		departmentRole.setMenus(Arrays.asList(department, departmentDepartment, departmentPosition, departmentStaff));
		roleRepository.save(departmentRole);
		// 用户：department
		User departmentUser = new User();
		departmentUser.setId("department");
		departmentUser.setLoginName("department");
		departmentUser.setPassword(WoUtil.getMD5(departmentUser.getId(), PWD));
		departmentUser.setCreateTime(new Date());
		departmentUser.setPasswordTime(new Date());
		departmentUser.setRoles(Arrays.asList(departmentRole));
		userRepository.save(departmentUser);
	}
}
